<!--
 * @Author: anshengtyp
 * @Date: 2023-12-15 10:00:21
 * @LastEditTime: 2023-12-15 10:35:15
 * @LastEditors: anshengtyp
 * @Description: 消息模版
-->
<!-- 流程管理 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="模版名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入模版名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增消息模版</el-button>
      </el-col>
      <right-toolbar :search="false" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="templateList">
      <el-table-column type="index" width="90" label="序号" align="center" />
      <el-table-column label="模版名称" align="center" prop="name" />
      <el-table-column label="模版标题" align="center" prop="titleTemplate" />
      <el-table-column label="模版内容" align="center" prop="contentTemplate" />
      <el-table-column label="模版类型" align="center" prop="key">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.xmgl_message_type" :value="scope.row.key"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="模版名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入模版名称" />
        </el-form-item>
        <el-form-item label="消息模版类型" prop="key">
          <el-select v-model="form.key" placeholder="请选择消息模版类型" style="width: 100%;" clearable filterable>
            <el-option v-for="item in dict.type.xmgl_message_type" :key="item.value" :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="消息模版标题" prop="titleTemplate">
          <el-input v-model="form.titleTemplate" placeholder="请输入消息模版标题" />
        </el-form-item>
        <el-form-item label="消息模版内容" prop="contentTemplate">
          <el-input type="textarea" :rows="4" v-model="form.contentTemplate" placeholder="请输入消息模版内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getTemplateList, addTemplateList, editTemplateList, delTemplateList, getTemplate } from "@/api/project/message";

export default {
  name: "MessageTemplate",
  dicts: ['xmgl_message_type'],
  data() {
    return {
      // 遮罩层
      // 总条数
      total: 0,
      // 表格数据
      templateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "消息模版名称不能为空", trigger: "blur" }
        ],
        key: [
          { required: true, message: "消息模版类型不能为空", trigger: "blur" }
        ],
        titleTemplate: [
          { required: true, message: "消息模版标题不能为空", trigger: "blur" }
        ],
        contentTemplate: [
          { required: true, message: "消息模版内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      getTemplateList(this.queryParams).then(response => {
        this.templateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = this.$options.data().form;
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加消息模版";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postId = row.id
      getTemplate(postId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消息模版";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            editTemplateList(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTemplateList(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除消息模版名称为"' + row.name + '"的数据项？').then(() => {
        return delTemplateList(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    }
  }
};
</script>

